package com.zygh.policeasset.service;

import io.minio.BucketExistsArgs;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveBucketArgs;
import io.minio.RemoveObjectArgs;
import io.minio.RemoveObjectsArgs;
import io.minio.Result;
import io.minio.SetBucketLifecycleArgs;
import io.minio.http.Method;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Expiration;
import io.minio.messages.LifecycleConfiguration;
import io.minio.messages.LifecycleRule;
import io.minio.messages.RuleFilter;
import io.minio.messages.Status;
import java.io.InputStream;
import java.time.ZonedDateTime;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/MinioService.class */
public class MinioService {
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    private MinioClient minioClient;

    @PostConstruct
    public void init() {
        try {
            this.minioClient = MinioClient.builder().endpoint(this.endpoint).credentials(this.accessKey, this.secretKey).build();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Boolean bucketExists(String bucketName) {
        try {
            return Boolean.valueOf(this.minioClient.bucketExists((BucketExistsArgs) BucketExistsArgs.builder().bucket(bucketName).build()));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean makeBucket(String bucketName) {
        try {
            this.minioClient.makeBucket((MakeBucketArgs) MakeBucketArgs.builder().bucket(bucketName).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean removeBucket(String bucketName) {
        try {
            this.minioClient.removeBucket((RemoveBucketArgs) RemoveBucketArgs.builder().bucket(bucketName).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean setBucketLifecycle(String bucketName, int days) {
        try {
            List<LifecycleRule> rules = new LinkedList<>();
            rules.add(new LifecycleRule(Status.ENABLED, null, new Expiration((ZonedDateTime) null, Integer.valueOf(days), (Boolean) null), new RuleFilter("/"), "life-rule", null, null, null));
            this.minioClient.setBucketLifecycle((SetBucketLifecycleArgs) SetBucketLifecycleArgs.builder().bucket(bucketName).config(new LifecycleConfiguration(rules)).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean upload(MultipartFile file, String bucketName) {
        try {
            this.minioClient.putObject((PutObjectArgs) ((PutObjectArgs.Builder) ((PutObjectArgs.Builder) PutObjectArgs.builder().bucket(bucketName)).object(file.getOriginalFilename())).stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Boolean upload(String fileName, InputStream inputStream, String contentType, String bucketName) {
        try {
            this.minioClient.putObject((PutObjectArgs) ((PutObjectArgs.Builder) ((PutObjectArgs.Builder) PutObjectArgs.builder().bucket(bucketName)).object(fileName)).stream(inputStream, Long.valueOf((long) inputStream.available()).longValue(), -1).contentType(contentType).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public String getObjectUrl(String bucketName, String objectName) throws Exception {
        String url = "";
        if (bucketExists(bucketName).booleanValue()) {
            url = this.minioClient.getPresignedObjectUrl((GetPresignedObjectUrlArgs) ((GetPresignedObjectUrlArgs.Builder) ((GetPresignedObjectUrlArgs.Builder) GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName)).object(objectName)).expiry(2, TimeUnit.DAYS).build());
        }
        return url;
    }

    /*  JADX ERROR: JadxRuntimeException in pass: BlockProcessor
        jadx.core.utils.exceptions.JadxRuntimeException: Unreachable block: B:17:0x00c9
        	at jadx.core.dex.visitors.blocks.BlockProcessor.checkForUnreachableBlocks(BlockProcessor.java:86)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.processBlocksTree(BlockProcessor.java:52)
        	at jadx.core.dex.visitors.blocks.BlockProcessor.visit(BlockProcessor.java:44)
        */
    public void download(java.lang.String r6, java.lang.String r7, javax.servlet.http.HttpServletResponse r8) {
        /*
            Method dump skipped, instructions count: 435
            To view this dump change 'Code comments level' option to 'DEBUG'
        */
        throw new UnsupportedOperationException("Method not decompiled: com.zygh.policeasset.service.MinioService.download(java.lang.String, java.lang.String, javax.servlet.http.HttpServletResponse):void");
    }

    public boolean removeObject(String bucketName, String object) {
        try {
            this.minioClient.removeObject((RemoveObjectArgs) ((RemoveObjectArgs.Builder) ((RemoveObjectArgs.Builder) RemoveObjectArgs.builder().bucket(bucketName)).object(object)).build());
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Iterable<Result<DeleteError>> removeObjects(String bucketName, List<String> objects) {
        return this.minioClient.removeObjects((RemoveObjectsArgs) RemoveObjectsArgs.builder().bucket(bucketName).objects((List) objects.stream().map(e -> {
            return new DeleteObject(e);
        }).collect(Collectors.toList())).build());
    }
}